package com.yaoyong.yy_demo.module.spring5.el;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

import java.io.IOException;

@Configuration
@ComponentScan("com.yaoyong.yy_demo.module.spring5.el")
@PropertySource("classpath:test.properties")
public class ElConfig {

    /**
     * 1.注入普通字符串
     */
    @Value("I Love You !")
    private String normal;

    /**
     * 2.注入操作系统属性
     */
    @Value("#{systemProperties['os.name']}")
    private String osName;

    /**
     * 3.注入表达式结果
     */
    @Value("#{T(Math).random()*100}")
    private double randomNumber;

    /**
     * 4.注入其他bean属性
     */
    @Value("#{demoService.another}")
    private String fromAnother;

    /**
     * 5.注入文件资源
     */
    @Value("classpath:test.txt")
    private Resource testFile;

    /**
     * 6.注入网址资源
     */
    @Value(("http://www.baidu.com"))
    private Resource testUrl;

    /**
     * 7.注入配置文件
     */

    @Value("${book.name}")
    private String bookName;

    @Autowired
    private Environment environment;


    public void outputResource() {
        try {
            System.out.println(normal);
            System.out.println(osName);
            System.out.println(randomNumber);
            System.out.println(fromAnother);
            System.out.println(IOUtils.toString(testFile.getInputStream(),"utf-8"));
            System.out.println(IOUtils.toString(testUrl.getInputStream(),"utf-8"));
            System.out.println(bookName);
            System.out.println(environment.getProperty("book.author"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
